﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.DataBase;
using System.Windows.Controls.Primitives;

namespace VAdvantage.Classes
{
    public class CommonFunctions
    {

        /// <summary>
        /// return Int value
        /// </summary>
        /// <param name="obj">value to convert</param>
        /// <returns></returns>
        /// <Author>Harwinder</Author> 
        public static int GetInt(object obj)
        {
            if (obj == null || obj.ToString() == "")
            {
                return 0;
            }
            return int.Parse(obj.ToString());
        }


        /// <summary>
        /// Return string value
        /// </summary>
        /// <param name="obj">value to convert</param>
        /// <returns>Object</returns>
        /// <Author>Harwinder</Author> 
        public static string GetString(object obj)
        {
            if (obj == null)
            {
                return "";
            }
            return obj.ToString();
        }

        public static long CurrentTimeMillis()
        {
            TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
            return (long)ts.TotalMilliseconds;
        }


        /// <summary>
        /// Gets time in mili seconds since 1970
        /// </summary>
        /// <param name="start">datetime</param>
        /// <returns>Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by passed object</returns>
        public static long CurrentTimeMillis(DateTime start)
        {
            TimeSpan ts = (start - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
            long mils = (long)ts.TotalMilliseconds;
            //long l = dt.Ticks / TimeSpan.TicksPerMillisecond;
           // DateTime myDate = new DateTime((mils * TimeSpan.TicksPerMillisecond) + 621355968000000000);
            return mils;
        }




        public static DateTime CovertMilliToDate(long milliseconds)
        {
            DateTime myDate = new DateTime((milliseconds * TimeSpan.TicksPerMillisecond) + 621355968000000000);
            return myDate;
        }

        public static void ShowMessage(string message, Controls.VStatusBar lblStatusBar)
        {
            lblStatusBar.lblStatusDB.Text = "";
            lblStatusBar.lblStatusDB.Text = message;
            lblStatusBar.DBForeColor = System.Windows.Media.Colors.Red;
        }

        private static int counter = -1;
        /// <summary>
        /// Generate random Number
        /// </summary>
        /// <returns>randam number</returns>
        public static int GenerateRandomNo()
        {
            if (counter == -1)
            {
                counter = new Random().Next() & 0xffff;
            }
            counter++;
            return counter;
        }

        public static void ShowErrorMessage(string statusText, Label lblStatus)
        {
            lblStatus.Content = "";
            lblStatus.Content = statusText;
            lblStatus.Foreground = new SolidColorBrush(Colors.Red);// System.Drawing.Color.Red;
        }
        //public static void ShowErrorMessage(String message, System.Windows.Forms.ToolStripLabel lbl)
        //{
        //    lbl.Text = "";
        //    lbl.Text = message;
        //    lbl.ForeColor = System.Drawing.Color.Red;
        //}
        /// <summary>
        /// Adds specified number of value to current datetime
        /// </summary>
        /// <param name="duration">integer number from CommonFuctions.Calendar enum</param>
        /// <param name="time"></param>
        /// <returns>new date</returns>
        /// <author>Veena</author>
        public static DateTime AddDate(int duration, object time)
        {
            if (duration == GlobalVariable.DayOfYear)
            {
                return DateTime.Now.AddDays(Convert.ToDouble(time));
            }
            else if (duration == GlobalVariable.Month)
            {
                return DateTime.Now.AddMonths(Utility.Util.GetValueOfInt(time.ToString()));
            }
            else if (duration == GlobalVariable.Hour)
            {
                return DateTime.Now.AddHours(Convert.ToDouble(time));
            }
            else if (duration == GlobalVariable.Minute)
            {
                return DateTime.Now.AddMinutes(Convert.ToDouble(time));
            }
            else if (duration == GlobalVariable.Second)
            {
                return DateTime.Now.AddSeconds(Convert.ToDouble(time));
            }
            else if (duration == GlobalVariable.Year)
            {
                return DateTime.Now.AddYears(Utility.Util.GetValueOfInt(time.ToString()));
            }
            return DateTime.Now;
        }
        /// <summary>
        /// Gets whether specified value is a number
        /// </summary>
        /// <param name="value">value</param>
        /// <returns>true if given string is a number</returns>
        /// <author>Veena</author>
        public static bool IsNumeric(string value)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(value, @"^-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?$");
        }


        /// <summary>
        /// Set grid style
        /// </summary>
        /// <param name="vdgvGrid">Grid Name whosse style has to be set</param>
        /// <author>Veena Pandey</author>
        public static void SetDataGridPrperties(SLControls.Controls.VDataGrid vdgvGrid)
        {
            //set grid style
            //vdgvGrid.MultiSelect = false;
            //vdgvGrid.DisplayGridRowNumber = true;
            //vdgvGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
           // vdgvGrid.AllowUserToAddRows = false;
           // vdgvGrid.AllowUserToResizeRows = false;
            //vdgvGrid.EnableHeadersVisualStyles = false;
            //vdgvGrid.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnEnter;
            vdgvGrid.Background = new SolidColorBrush(GlobalVariable.GRID_BACK_COLOR);

            Style style1 = new Style(typeof(DataGrid));
            
            style1.Setters.Add(new Setter(DataGridColumnHeader.MinWidthProperty,100));
            style1.Setters.Add(new Setter(DataGridColumnHeader.BackgroundProperty,new SolidColorBrush(GlobalVariable.GRID_HEADERBACK_COLOR)));

            style1.Setters.Add(new Setter(DataGridRowHeader.BackgroundProperty, new SolidColorBrush(GlobalVariable.GRID_HEADERBACK_COLOR)));


            
            style1.Setters.Add(new Setter(DataGridCell.BackgroundProperty, new SolidColorBrush( GlobalVariable.GRID_DEFAULTBACK_COLOR)));//     HeightProperty, 15));

            vdgvGrid.Style = style1;

            Style style2 = new Style(typeof(DataGridColumn));

            style2.Setters.Add(new Setter(DataGridColumnHeader.FontWeightProperty, FontWeights.Bold));

            //vdgvGrid.ColumnHeaderStyle = style2;

            vdgvGrid.RowHeight = 25;
            vdgvGrid.ColumnHeaderHeight = 30;

            //vdgvGrid.ColumnHeaderStyle   .DefaultCellStyle.BackColor = GlobalVariable.GRID_DEFAULTBACK_COLOR;
            //vdgvGrid.AlternatingRowsDefaultCellStyle.BackColor =GlobalVariable.GRID_DEFAULTALTERNATEBACK_COLOR;
            //vdgvGrid.DefaultCellStyle.ForeColor = GlobalVariable.GRID_DEFAULTFORE_COLOR;
            vdgvGrid.SelectionMode = DataGridSelectionMode.Single;
            
            
            vdgvGrid.AlternatingRowBackground = new SolidColorBrush(GlobalVariable.GRID_DEFAULTALTERNATEBACK_COLOR);

            //vdgvGrid.ColumnHeadersDefaultCellStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.NotSet;
            //vdgvGrid.ColumnHeadersDefaultCellStyle.BackColor = GlobalVariable.GRID_HEADERBACK_COLOR;
            //vdgvGrid.ColumnHeadersDefaultCellStyle.ForeColor = GlobalVariable.GRID_HEADERFORE_COLOR;


            //vdgvGrid.RowHeadersDefaultCellStyle.BackColor = GlobalVariable.GRID_HEADERBACK_COLOR;
            //vdgvGrid.RowHeadersDefaultCellStyle.ForeColor = GlobalVariable.GRID_HEADERFORE_COLOR;
            //vdgvGrid.TopLeftHeaderCell.Style.BackColor = GlobalVariable.GRID_HEADERBACK_COLOR;
            //vdgvGrid.RowHeadersDefaultCellStyle.SelectionBackColor = GlobalVariable.GRID_HEADERBACK_COLOR;


            //vdgvGrid.GridColor = GlobalVariable.GRID_LINE_COLOR;
            //vdgvGrid.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Raised;
            //vdgvGrid.ColumnHeadersDefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            //vdgvGrid.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("MS Sans Serif", 9, System.Drawing.FontStyle.Bold);
            //vdgvGrid.ColumnHeadersHeight = 23;
            //vdgvGrid.RowTemplate.Height = 18;

        }

    }
}
